package com.theyeasy.dvmini.pc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.theyeasy.weixin.service.WxMiniService;
import org.theyeasy.weixin.service.WxOpenMpService;
import org.theyeasy.weixin.service.WxOpenService;
import org.theyeasy.weixin.util.WxMpUtil;

import com.theyeasy.dvmini.model.SysWxauthorize;
import com.theyeasy.dvmini.service.SysWxauthorizeService;
import com.theyeasy.dvmini.util.RedisUtil;
import com.theyeasy.dvmini.vo.Vo_msg;

@Controller
@RequestMapping("/pcwx")
public class PcWxController {
	@Autowired
	WxOpenService wxOpenService;
	
	
	
	@Autowired
	SysWxauthorizeService sysWxauthorizeService;
	
	
	@RequestMapping("/wxmanage")
	public ModelAndView wxManage() {
		ModelAndView mv = new ModelAndView("/pc/wx/wxmanage");

		//获取用以公众号授权的二维码
		String url="none";

		try {
			url = wxOpenService.getOauth2buildAuthorizationUrl();
		} catch (Exception e) {
			e.printStackTrace();
		}

		mv.addObject("url",url);
		 	
		return mv;
	}
	
	/*显示已授权的公众号*/
	@RequestMapping("/getwx")
	public @ResponseBody Vo_msg getWx()
	{
		SysWxauthorize sysWxauthorize = sysWxauthorizeService.getAuthorizerOnlyOne();		
		return new Vo_msg(0, sysWxauthorize);
	}
	
	/*判断模板消息是否存在,有长ID表存公众号添加过对应的模板消息*/
	@RequestMapping("/getTemplate")
	public @ResponseBody Vo_msg getTemplateLongId(String template_id_short)
	{		
		return new Vo_msg(0,"",WxMpUtil.getTemplateLongId(template_id_short));
	}
	
	
	/*添加模板消息*/
	@RequestMapping("/addTemplate")
	public @ResponseBody Vo_msg addTemplate(String template_id_short)
	{
		String msg = WxMpUtil.addTemplate(template_id_short);
		return new Vo_msg(0,"",msg);
	}
	
	/*测试发送模板消息*/
	@RequestMapping("/testSendTemplate")
	public @ResponseBody String testSendTemplate(String mpOpenid)
	{
		String result = WxMpUtil.SendTemplate_Vanker(mpOpenid, "你好，请问东莞万科的楼盘平均价格多少？首付会很高吗？月供5000以内的有没有？在线等，请尽快答复！", "admin", "无", "2017-10-28 21:33:31", "mini.weiyisz.com/wx/chatListView?salesId=1");
		return result;
	}
}
